package es.kix2902.testing.android.testing;

import android.app.Activity;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.RemoteViews;
import es.kix2902.testing.android.R;

public class WidgetSettings extends Activity {

	int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;

	public static final int BUTTON_GOOGLE = 0;
	public static final int BUTTON_GMAIL = 1;

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);

		setResult(RESULT_CANCELED);
		setContentView(R.layout.widgetsetting_lay);

		Intent intent = getIntent();
		Bundle extras = intent.getExtras();
		if (extras != null) {
			mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
		}

		findViewById(R.id.btnSave).setOnClickListener(okClickListener);
		findViewById(R.id.btnCancel).setOnClickListener(cancelClickListener);

		if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
			finish();
		}
	}

	private OnClickListener cancelClickListener = new OnClickListener() {
		public void onClick(View v) {
			finish();
		}        
	};


	private OnClickListener okClickListener = new OnClickListener() {
		public void onClick(View v) {
			Context context = v.getContext();
			SharedPreferences.Editor prefs = context.getSharedPreferences("widget", 0).edit();

			CheckBox chk1 = (CheckBox) findViewById(R.id.chk_01); 
			prefs.putBoolean("chk1" + mAppWidgetId, chk1.isChecked());

			CheckBox chk2 = (CheckBox) findViewById(R.id.chk_02); 
			prefs.putBoolean("chk2" + mAppWidgetId, chk2.isChecked());

			prefs.commit();

			AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

			RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_lay);

			if (chk1.isChecked())
			{
				views.setViewVisibility(R.id.btn_01, View.VISIBLE);

				Intent i = new Intent();
				i.setClass(context, WidgetSettings.class);
				i.addCategory(Intent.CATEGORY_ALTERNATIVE);
				i.setData(Uri.parse("custom:" + BUTTON_GOOGLE));

				PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);

				views.setOnClickPendingIntent(R.id.btn_02, pi);
			}

			if (chk2.isChecked())
			{
				views.setViewVisibility(R.id.btn_02, View.VISIBLE);

				Intent i = new Intent();
				i.setClass(context, WidgetSettings.class);
				i.addCategory(Intent.CATEGORY_ALTERNATIVE);
				i.setData(Uri.parse("custom:" + BUTTON_GMAIL));

				PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);

				views.setOnClickPendingIntent(R.id.btn_02, pi);
			}

			appWidgetManager.updateAppWidget(mAppWidgetId, views);

			Intent result = new Intent();
			result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
			setResult(RESULT_OK, result);
			finish();
		}
	};
}